package com.openbravo.pos.scale;

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;

public class ScaleSamsungEsp
  implements Scale, SerialPortEventListener
{
  private CommPortIdentifier m_PortIdPrinter;
  private SerialPort m_CommPortPrinter;
  private String m_sPortScale;
  private OutputStream m_out;
  private InputStream m_in;
  private static final int SCALE_READY = 0;
  private static final int SCALE_READING = 1;
  private static final int SCALE_READINGDECIMALS = 2;
  private double m_dWeightBuffer;
  private double m_dWeightDecimals;
  private int m_iStatusScale;

  public ScaleSamsungEsp(String paramString)
  {
    this.m_sPortScale = paramString;
    this.m_out = null;
    this.m_in = null;
    this.m_iStatusScale = 0;
    this.m_dWeightBuffer = 0.0D;
    this.m_dWeightDecimals = 1.0D;
  }

  public Double readWeight()
  {
    synchronized (this)
    {
      if (this.m_iStatusScale != 0)
      {
        try
        {
          wait(1000L);
        }
        catch (InterruptedException localInterruptedException1)
        {
        }
        if (this.m_iStatusScale != 0)
          this.m_iStatusScale = 0;
      }
      this.m_dWeightBuffer = 0.0D;
      this.m_dWeightDecimals = 1.0D;
      write(new byte[] { 36 });
      flush();
      try
      {
        wait(1000L);
      }
      catch (InterruptedException localInterruptedException2)
      {
      }
      if (this.m_iStatusScale == 0)
      {
        double d = this.m_dWeightBuffer / this.m_dWeightDecimals;
        this.m_dWeightBuffer = 0.0D;
        this.m_dWeightDecimals = 1.0D;
        return new Double(d);
      }
      this.m_iStatusScale = 0;
      this.m_dWeightBuffer = 0.0D;
      this.m_dWeightDecimals = 1.0D;
      return new Double(0.0D);
    }
  }

  private void flush()
  {
    try
    {
      this.m_out.flush();
    }
    catch (IOException localIOException)
    {
    }
  }

  private void write(byte[] paramArrayOfByte)
  {
    try
    {
      if (this.m_out == null)
      {
        this.m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(this.m_sPortScale);
        this.m_CommPortPrinter = ((SerialPort)this.m_PortIdPrinter.open("PORTID", 2000));
        this.m_out = this.m_CommPortPrinter.getOutputStream();
        this.m_in = this.m_CommPortPrinter.getInputStream();
        this.m_CommPortPrinter.addEventListener(this);
        this.m_CommPortPrinter.notifyOnDataAvailable(true);
        this.m_CommPortPrinter.setSerialPortParams(4800, 8, 1, 1);
      }
      this.m_out.write(paramArrayOfByte);
    }
    catch (NoSuchPortException localNoSuchPortException)
    {
      localNoSuchPortException.printStackTrace();
    }
    catch (PortInUseException localPortInUseException)
    {
      localPortInUseException.printStackTrace();
    }
    catch (UnsupportedCommOperationException localUnsupportedCommOperationException)
    {
      localUnsupportedCommOperationException.printStackTrace();
    }
    catch (TooManyListenersException localTooManyListenersException)
    {
      localTooManyListenersException.printStackTrace();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
  }

  public void serialEvent(SerialPortEvent paramSerialPortEvent)
  {
    switch (paramSerialPortEvent.getEventType())
    {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
      break;
    case 1:
      try
      {
        while (this.m_in.available() > 0)
        {
          int i = this.m_in.read();
          if (i == 13)
          {
            synchronized (this)
            {
              this.m_iStatusScale = 0;
              notifyAll();
            }
          }
          else if (((i > 47) && (i < 58)) || (i == 46))
          {
            synchronized (this)
            {
              if (this.m_iStatusScale == 0)
              {
                this.m_dWeightBuffer = 0.0D;
                this.m_dWeightDecimals = 1.0D;
                this.m_iStatusScale = 1;
              }
              if (i == 46)
              {
                this.m_iStatusScale = 2;
              }
              else
              {
                this.m_dWeightBuffer = (this.m_dWeightBuffer * 10.0D + i - 48.0D);
                if (this.m_iStatusScale == 2)
                  this.m_dWeightDecimals *= 10.0D;
              }
            }
          }
          else
          {
            this.m_dWeightBuffer = 0.0D;
            this.m_dWeightDecimals = 1.0D;
            this.m_iStatusScale = 0;
          }
        }
      }
      catch (IOException localIOException)
      {
      }
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.scale.ScaleSamsungEsp
 * JD-Core Version:    0.6.0
 */